local xiaowu = fk.CreateSkill {

  name = "joy__xiaowu",

  tags = {  },

}



xiaowu:addEffect("active", {
  name = "joy__xiaowu",
  anim_type = "offensive",
  prompt = "#xiaowu",
  max_card_num = 0,
  target_num = 1,
  interaction = function(self,player)
    return UI.ComboBox { choices = {"xiaowu_clockwise", "xiaowu_anticlockwise"} }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(xiaowu.name, Player.HistoryPhase) < 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local players = room:getOtherPlayers(player)
    local targets = {}
    local choice = self.interaction.data
    for i = 1, #players, 1 do
      local real_i = i
      if choice == "xiaowu_anticlockwise" then
        real_i = #players + 1 - real_i
      end
      local temp = players[real_i]
      table.insert(targets, temp)
      if temp == target then break end
    end
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    local x = 0
    local to_damage = {}
    for _, p in ipairs(targets) do
      if not p.dead and not player.dead then
        choice = room:askForChoice(p, {"xiaowu_draw1", "draw1"}, xiaowu.name, "#xiawu_draw:" .. player.id)
        if choice == "xiaowu_draw1" then
          player:drawCards(1, xiaowu.name)
          x = x+1
        elseif choice == "draw1" then
          p:drawCards(1, xiaowu.name)
          table.insert(to_damage, p.id)
        end
      end
    end
    if not player.dead then
      local n = 0
      if x > 0 then
        n = x%2
        n = n + x//2
      end
      room:addPlayerMark(player, "@xiaowu_sand",n)
      if x < #to_damage then
        room:sortPlayersByAction(to_damage)
        for _, pid in ipairs(to_damage) do
          local p = room:getPlayerById(pid)
          if not p.dead then
            room:damage{ from = player, to = p, damage = 1, skillName = xiaowu.name }
          end
        end
      end
    end
  end,
})

return xiaowu